package com.kkshop.test.fixture;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.PreparedStatementCreator;

public class NewUserFixture implements Fixture {
	
	private static final String INSERT_USER_SQL = "INSERT INTO users VALUES (?, ?, ?)";
	private static final String INSERT_AUTHORITY_SQL = "INSERT INTO authorities VALUES (?, ?)";
	private static final String DELETE_USER_SQL = "DELETE FROM users WHERE username = ?";
	private static final String DELETE_AUTHORITY_SQL = "DELETE FROM authorities WHERE username = ?";
	
	protected DataSource dataSource;
	protected String username;
	protected String password;
	protected String role;
	protected boolean enabled;
	
	public NewUserFixture(DataSource dataSource, String username, String password, String role, boolean enabled) {
		this.dataSource = dataSource;
		this.username = username;
		this.password = password;
		this.role = role;
		this.enabled = enabled;
	}

	public void setUp() throws Exception {
		JdbcTemplate template = new JdbcTemplate(dataSource);
		template.execute(new PreparedStatementCreator() {
			public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
				PreparedStatement statement = connection.prepareStatement(INSERT_USER_SQL);
				statement.setString(1, username);
				statement.setString(2, password);
				statement.setBoolean(3, enabled);
				return statement;
			}
		}, new PreparedStatementCallback() {
			public Object doInPreparedStatement(PreparedStatement statement) throws SQLException, DataAccessException {
				statement.execute();
				return null;
			}
		});
		template.execute(new PreparedStatementCreator() {
			public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
				PreparedStatement statement = connection.prepareStatement(INSERT_AUTHORITY_SQL);
				statement.setString(1, username);
				statement.setString(2, role);
				return statement;
			}
		}, new PreparedStatementCallback() {
			public Object doInPreparedStatement(PreparedStatement statement) throws SQLException, DataAccessException {
				statement.execute();
				return null;
			}
		});
	}

	public void tearDown() throws Exception {
		JdbcTemplate template = new JdbcTemplate(dataSource);
		template.execute(new PreparedStatementCreator() {
			public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
				PreparedStatement statement = connection.prepareStatement(DELETE_AUTHORITY_SQL);
				statement.setString(1, username);
				return statement;
			}
		}, new PreparedStatementCallback() {
			public Object doInPreparedStatement(PreparedStatement statement) throws SQLException, DataAccessException {
				statement.execute();
				return null;
			}
		});
		template.execute(new PreparedStatementCreator() {
			public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
				PreparedStatement statement = connection.prepareStatement(DELETE_USER_SQL);
				statement.setString(1, username);
				return statement;
			}
		}, new PreparedStatementCallback() {
			public Object doInPreparedStatement(PreparedStatement statement) throws SQLException, DataAccessException {
				statement.execute();
				return null;
			}
		});
	}
}
